<?php

/**
 * uniBoard ACP Users Search... and List, ofcourse
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-20 21:23:27 +0000 (Mon, 20 Sep 2010) $
 * $Revision: 64 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_usearch{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * URL Base
	 *
	 * @var string
	 */
	
	private $url_base		= 'act=usearch&st=%s';
	
	/**
	 * Avaiable groups?
	 *
	 * @var array
	 */
	
	private $groups			= array();
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Get avaiable groups
		$this -> groups = $appCore -> users -> getUserGroups();
		
		// Load fields templates
		$appCore -> templates['form_inputs'] = new tpl_form_inputs( $appCore);
		
		// Cache output
		ob_start();
		
		// Do Search?
		if ( $appCore -> _REQ( 'search') == 1)
		{
			// Do Search
			$this -> doSearch();
		}
		
		// Draw Form
		$this -> drawForm();
		
		// Draw output
		$appCore -> output -> draw( $appCore -> lang['acp_users_search'], array( $appCore -> adminLink( $this -> url_base) => $appCore -> lang['acp_users_search']), ob_get_clean());
				
	}

	// ------------------------------------------------------------------------

	/**
	 * Does Search and displays results
	 *
	 */
	
	function doSearch(){
		
		// Try-Catch
		try
		{
			// Set Empty search SQL
			$search_sql = '';
			
			// Start "search" URL
			$this -> url_base .= '&search=1';
			
			// Find ID?
			if ( $this -> appCore -> _REQ( 'fid') > 0)
			{
				// Put in SQL
				$search_sql .= ' AND u.user_id = \'' . ((integer) $this -> appCore -> _REQ( 'fid')) . '\'';
				
				// Put in URL
				$this -> url_base .= '&fid=' . ((integer) $this -> appCore -> _REQ( 'fid'));
			}
			
			// Find in Login?
			if ( strlen( $this -> appCore -> _REQ( 'flogin')) > 0)
			{
				// Put in SQL
				$search_sql .= ' AND u.user_login LIKE \'' . str_replace( '*', '%', $this -> appCore -> db -> strEscape($this -> appCore -> _REQ( 'flogin'), true)) . '\'';
				
				// Put in URL
				$this -> url_base .= '&flogin=' . class_strings::sprintfEnc( $this -> appCore -> _REQ( 'flogin'));
			}
			
			// Find in Name?
			if ( strlen( $this -> appCore -> _REQ( 'fname')) > 0)
			{
				// Put in SQL
				$search_sql .= ' AND u.user_name LIKE \'' . str_replace( '*', '%', $this -> appCore -> db -> strEscape($this -> appCore -> _REQ( 'fname'), true)) . '\'';
				
				// Put in URL
				$this -> url_base .= '&fname=' . class_strings::sprintfEnc( $this -> appCore -> _REQ( 'fname'));
			}
			
			// Find in Login?
			if ( strlen( $this -> appCore -> _REQ( 'fmail')) > 0)
			{
				// Put in SQL
				$search_sql .= ' AND u.user_email LIKE \'' . str_replace( '*', '%', $this -> appCore -> db -> strEscape($this -> appCore -> _REQ( 'fmail'), true)) . '\'';
				
				// Put in URL
				$this -> url_base .= '&fmail=' . class_strings::sprintfEnc( $this -> appCore -> _REQ( 'fmail'));
			}
			
			// Find in Group in GET?
			if ( strlen( $this -> appCore -> _GET( 'fgroup')) > 0)
			{
				// Put in SQL
				$search_sql .= ' AND u.group_id IN (' . class_strings::stripBrackets( $this -> appCore -> db -> strEscape($this -> appCore -> _REQ( 'fgroup'))) . ')';
				
				// Put in URL
				$this -> url_base .= '&fgroup=' . class_strings::sprintfEnc( $this -> appCore -> _GET( 'fgroup'));
			}
			else if ( count( $this -> appCore -> listInput( 'fgroup')) > 0)
			{
				// Put in SQL
				$search_sql .= ' AND u.group_id IN (' . join( ',', $this -> appCore -> listInput( 'fgroup')) . ')';
				
				// Put in URL
				$this -> url_base .= '&fgroup=' . class_strings::sprintfEnc( join( ',', $this -> appCore -> listInput( 'fgroup')));
			}
			
			// Find in Group in GET?
			if ( strlen( $this -> appCore -> _GET( 'fogroup')) > 0)
			{
				// Put in SQL
				$search_sql .= ' AND gm.group_id IN (' . class_strings::stripBrackets( $this -> appCore -> db -> strEscape($this -> appCore -> _REQ( 'fogroup'))) . ')';
				
				// Put in URL
				$this -> url_base .= '&fogroup=' . class_strings::sprintfEnc( $this -> appCore -> _GET( 'fogroup'));
			}
			else if ( count( $this -> appCore -> listInput( 'fogroup')) > 0)
			{
				// Put in SQL
				$search_sql .= ' AND gm.group_id IN (' . join( ',', $this -> appCore -> listInput( 'fogroup')) . ')';
				
				// Put in URL
				$this -> url_base .= '&fogroup=' . class_strings::sprintfEnc( join( ',', $this -> appCore -> listInput( 'fogroup')));
			}
			
			// Find in Activation in GET?
			if ( strlen( $this -> appCore -> _GET( 'fact')) > 0)
			{
				// Put in SQL
				$search_sql .= ' AND u.user_activation IN (' . class_strings::stripBrackets( $this -> appCore -> db -> strEscape($this -> appCore -> _REQ( 'fact'))) . ')';
				
				// Put in URL
				$this -> url_base .= '&fact=' . class_strings::sprintfEnc( $this -> appCore -> _GET( 'fact'));
			}
			else if ( count( $this -> appCore -> listInput( 'fact')) > 0)
			{
				// Put in SQL
				$search_sql .= ' AND u.user_activation IN (' . join( ',', $this -> appCore -> listInput( 'fact')) . ')';
				
				// Put in URL
				$this -> url_base .= '&fact=' . class_strings::sprintfEnc( join( ',', $this -> appCore -> listInput( 'fact')));
			}
			
			// Nothing to Search?
			if ( !isset( $search_sql[0]))
			{
				throw new exc_small( $this -> appCore -> lang['acp_users_search_empty'], $this -> appCore -> lang['acp_users_search']);	
			}
			
			// Count Users to display
			$users_num = $this -> appCore -> db -> count( 'users u LEFT JOIN ' . DB_PREFIX . 'user_groups_members gm ON u.user_id = gm.user_id', 'u.user_id > \'0\'' . $search_sql);
			
			// Nothing found?
			if ( $users_num == 0)
			{
				throw new exc_small( $this -> appCore -> lang['acp_users_search_nothing'], $this -> appCore -> lang['acp_users_search']);	
			}
			
			// Make paginator
			$paginator = new class_paginator( $this -> appCore, $this -> appCore -> adminLink( $this -> url_base), $users_num, 15);
			
			// Draw Paginator
			echo $paginator -> draw();
			
			// Open table block
			echo $this -> appCore -> output -> openTableBlock( $this -> appCore -> lang['acp_found_users_form'] . $this -> appCore -> number( $users_num));
			
			// Draw header
			echo $this -> appCore -> output -> tableHeader( array(
				array(
					'left_float width_icon',
					''
				),
				array(
					'left_float width_medium',
					$this -> appCore -> lang['column_user']
				),
				array(
					'right_float width_medium',
					$this -> appCore -> lang['actions']
				),
				array(
					'right_float width_big',
					$this -> appCore -> lang['column_group']
				)
			));
				
			// Select Users
			$this -> appCore -> db -> query( 'SELECT DISTINCT u.user_id, u.user_login, u.user_name, u.user_email, u.user_avatar_type, u.user_avatar_image, u.user_avatar_width, u.user_avatar_height, g.group_name, g.group_prefix, g.group_suffix
			FROM ' . DB_PREFIX . 'users u
			LEFT JOIN ' . DB_PREFIX . 'user_groups g ON  u.group_id = g.group_id
			LEFT JOIN ' . DB_PREFIX . 'user_groups_members gm ON  gm.user_id = u.user_id
			WHERE u.user_id > \'0\'' . $search_sql . '
			ORDER BY u.user_name
			LIMIT ' . $paginator -> getPosition() . ',15');
			
			// Parse result
			while ( $user_result = $this -> appCore -> db -> fetch_assoc())
			{				
				// Draw row
				echo $this -> appCore -> output -> tableRow('<table>
		<tr>
			<td class="cell_pad width_icon">
				' . ( $user_result['user_avatar_type'] > 0 ? '<img src="' . class_users::avatarLocation($user_result['user_avatar_type']) . htmlspecialchars($user_result['user_avatar_image']) . '" alt="' . htmlspecialchars( $this -> appCore -> lang['user_avatar']) . '" title="' . htmlspecialchars( $this -> appCore -> lang['user_avatar']) . '" ' . $this -> appCore -> scaleImage( $user_result['user_avatar_width'], $user_result['user_avatar_height'], 32, 32, false) . ' />' : $this -> appCore -> output -> drawImage( 'user_noimage', $this -> appCore -> lang['user_no_avatar'])) . '
			</td>
			<td class="cell_pad left_border">
				<a href="' . $this -> appCore -> niceLink( 'act=user&user=' . $user_result['user_id'], $user_result['user_name']) . '"><b>' . $user_result['user_name'] . '</b></a> <b class="item_append">' . htmlspecialchars( $user_result['user_login']) . '</b>
				<div class="item_description">' . htmlspecialchars( $user_result['user_email']) . '</div>
			</td>
			<td class="cell_pad left_border width_big">
				' . $user_result['group_prefix'] . $this -> appCore -> ifString( htmlspecialchars( $user_result['group_name'])) . $user_result['group_suffix'] . '
			</td>
			<td class="cell_pad left_border width_medium">
				' . $this -> appCore -> output -> toolsPopup( '<div><b>' . $this -> appCore -> lang['acp_user_op_sect_details'] . '</b></div>
	<a href="' . $this -> appCore -> adminLink( 'act=edit_user&do=profile&user=' . $user_result['user_id']) . '">' . $this -> appCore -> output -> drawImage( 'action_profile', $this -> appCore -> lang['acp_user_op_profile']) . ' ' . $this -> appCore -> lang['acp_user_op_profile'] . '</a>
	<a href="' . $this -> appCore -> adminLink( 'act=edit_user&do=name&user=' . $user_result['user_id']) . '">' . $this -> appCore -> output -> drawImage( 'action_edit', $this -> appCore -> lang['acp_user_op_names']) . ' ' . $this -> appCore -> lang['acp_user_op_names'] . '</a>
	<a href="' . $this -> appCore -> adminLink( 'act=edit_user&do=signature&user=' . $user_result['user_id']) . '">' . $this -> appCore -> output -> drawImage( 'action_edit', $this -> appCore -> lang['acp_user_op_signature']) . ' ' . $this -> appCore -> lang['acp_user_op_signature'] . '</a>
	<a href="' . $this -> appCore -> adminLink( 'act=edit_user&do=pass&user=' . $user_result['user_id']) . '">' . $this -> appCore -> output -> drawImage( 'action_pass', $this -> appCore -> lang['acp_user_op_pass']) . ' ' . $this -> appCore -> lang['acp_user_op_pass'] . '</a>
	<div><b>' . $this -> appCore -> lang['acp_user_op_sect_tools'] . '</b></div>
	<a href="' . $this -> appCore -> adminLink( 'act=edit_user&do=prune_keys&user=' . $user_result['user_id']) . '">' . $this -> appCore -> output -> drawImage( 'action_tool', $this -> appCore -> lang['acp_user_op_prune_remember']) . ' ' . $this -> appCore -> lang['acp_user_op_prune_remember'] . '</a>
	<a href="' . $this -> appCore -> adminLink( 'act=edit_user&do=prune_posts&user=' . $user_result['user_id']) . '">' . $this -> appCore -> output -> drawImage( 'action_tool', $this -> appCore -> lang['acp_user_op_prune_posts']) . ' ' . $this -> appCore -> lang['acp_user_op_prune_posts'] . '</a>
	<div><b>' . $this -> appCore -> lang['acp_user_op_sect_delete'] . '</b></div>
	<a href="' . $this -> appCore -> adminLink( 'act=edit_user&do=delete&user=' . $user_result['user_id']) . '">' . $this -> appCore -> output -> drawImage( 'action_delete', $this -> appCore -> lang['acp_user_op_delete_user']) . ' ' . $this -> appCore -> lang['acp_user_op_delete_user'] . '</a>') . '
			</td>
		</tr>
	</table>');
			}
			
			// Free result
			$this -> appCore -> db -> freeResult();
			
			// Close table block
			echo $this -> appCore -> output -> closeTableBlock();
			
			// Draw Paginator
			echo $paginator -> draw();
		}
		catch ( exc_small $exception)
		{
			// Draw Error
			$this -> appCore -> output -> error( $exception -> message, $exception -> title);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws Form
	 *
	 */
	
	function drawForm(){
		
		// Open Form
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['acp_search_users_form'], $this -> appCore -> lang['acp_search_users_form_expl']);
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=usearch&search=1'));
		
		// Draw Fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_search_users_find_id'], $this -> appCore -> drawTextInput( 'fid', $this -> appCore -> _REQ( 'fid')));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_search_users_find_login'], $this -> appCore -> drawTextInput( 'flogin', $this -> appCore -> _REQ( 'flogin')));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_search_users_find_name'], $this -> appCore -> drawTextInput( 'fname', $this -> appCore -> _REQ( 'fname')));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_search_users_find_email'], $this -> appCore -> drawTextInput( 'fmail', $this -> appCore -> _REQ( 'fmail')));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_search_users_find_group'], $this-> appCore -> drawMultiList( 'fgroup', $this -> groups, ( strlen( $this -> appCore -> _GET( 'fgroup')) > 0 ? explode( ',', $this -> appCore -> _GET( 'fgroup')) : $this -> appCore -> listInput( 'fgroup'))));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_search_users_find_groups'], $this-> appCore -> drawMultiList( 'fogroup', $this -> groups, ( strlen( $this -> appCore -> _GET( 'fogroup')) > 0 ? explode( ',', $this -> appCore -> _GET( 'fogroup')) : $this -> appCore -> listInput( 'fogroup'))));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_search_users_find_active'], $this-> appCore -> drawMultiList( 'fact', array(
			0 => $this -> appCore -> lang['acp_search_users_find_active_0'],
			1 => $this -> appCore -> lang['acp_search_users_find_active_1'],
			2 => $this -> appCore -> lang['acp_search_users_find_active_2']
		), ( strlen( $this -> appCore -> _GET( 'fact')) > 0 ? explode( ',', $this -> appCore -> _GET( 'fact')) : $this -> appCore -> listInput( 'fact'))));
		
		// Close Block
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['send']);
		echo $this -> appCore -> output -> closeForm();
		echo $this -> appCore -> output -> closeFormBlock();
		
	}
	
}